package com.megvii.koala.tv.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.tv.R;
import com.megvii.koala.tv.adapter.DisplayCameraSettingAdapter;
import com.megvii.koala.tv.data.CameraBean;
import com.megvii.koala.tv.presenter.DisplayCameraSettingPresenter;

import java.util.List;

import butterknife.Bind;
import butterknife.OnItemClick;

/**
 * Created by linyue on 16/3/16.
 */
public class DisplayCameraSettingFragment extends BaseFragment
{
    @Bind(R.id.display_camera_list)
    protected ListView listView;

    private DisplayCameraSettingAdapter adapter;

    private DisplayCameraSettingListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.display_camera_layout, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        adapter = new DisplayCameraSettingAdapter(getActivity());
        listView.setAdapter(adapter);
    }

    public void updateView(List<CameraBean> cameras)
    {
        adapter.updateData(cameras);
    }

    @OnItemClick(R.id.display_camera_list)
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        if (listener != null)
        {

            CameraBean camera = (CameraBean) parent.getItemAtPosition(position);
            listener.onClickSave(camera);
        }
    }

    @Override
    public Class<? extends KoalaPresenter> getPresenterClass()
    {
        return DisplayCameraSettingPresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    @Override
    public BaseFragmentListener getBaseFragmentListener()
    {
        return listener;
    }

    public void setListener(DisplayCameraSettingListener listener)
    {
        this.listener = listener;
    }

    public interface DisplayCameraSettingListener extends BaseFragmentListener
    {
        public void onClickSave(CameraBean camera);
    }
}
